package com.sky.service.impl;

import com.alibaba.druid.sql.PagerUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Transactional
    @Override
    public void save(DishDTO dishDTO) {
        // 设计多张表  开启事务

        // 构造菜品基本信息 保存数据到 菜品 dish 表中
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insert(dish);

        // 构造口味列表信息，保存数据到口味 dish_flavor 表中
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dish.getId());
            });

            dishFlavorMapper.insertBetch(flavors);
        }
    }

    /**
     * 分页查询 -- 基于分页插件
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());

        Page<DishVO> dishVOPage = dishMapper.pageQuery(dishPageQueryDTO);

        return new PageResult(dishVOPage.getTotal(), dishVOPage.getResult());
    }

    @Transactional
    @Override
    public void delete(List<Long> ids) {
        // 起售中的菜品不能删除
        ids.forEach(id -> {
            Dish dish = dishMapper.selectById(id);
            if (dish.getStatus().equals(StatusConstant.ENABLE)) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });

        // 套餐中是否有要删除的菜品
        ids.forEach(id -> {
            Integer count = setmealDishMapper.countByDishId(id);
            if (count > 0) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
        });

        // 删除菜品
        dishMapper.deleteBatch(ids);

        // 删除菜品口味
        dishFlavorMapper.deleteBatch(ids);

    }

    @Override
    public DishVO getDish(Long id) {

        DishVO dishVO = new DishVO();

        // 查询菜品基本信息
        Dish dish = dishMapper.selectById(id);
        BeanUtils.copyProperties(dish, dishVO);

        // 查询口味列表
        List<DishFlavor> flavors = dishFlavorMapper.selectByDishId(id);
        dishVO.setFlavors(flavors);

        return dishVO;
    }

    @Transactional
    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.update(dish);

        // 先删除之前的 再重新保存
        dishFlavorMapper.deleteByDishId(dishDTO.getId());

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            // 重新关联id
            flavors.forEach(flavor -> {
                flavor.setDishId(dishDTO.getId());
            });
            dishFlavorMapper.insertBetch(flavors);
        }

    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .build();
        dishMapper.update(dish);
    }

    @Override
    public List<Dish> list(Dish dish) {
        return dishMapper.list(dish);
    }

    @Override
    public List<DishVO> listAndFalvorByCategoryId(Dish dish) {
        List<DishVO> dishVOS = new ArrayList<>();

        List<Dish> dishs = dishMapper.list(dish);
        dishs.forEach(dish1 -> {
            // 根据 菜品 获取 口味列表
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish1, dishVO);

            // 设置口味列表
            List<DishFlavor> flavors = dishFlavorMapper.selectByDishId(dish1.getId());
            dishVO.setFlavors(flavors);

            dishVOS.add(dishVO);
        });

        return dishVOS;
    }

}

